package cn.itcast.controller.cargo;

import cn.itcast.controller.BaseController;
import cn.itcast.domain.cargo.*;
import cn.itcast.service.cargo.ContractProductService;
import cn.itcast.service.cargo.ContractService;
import cn.itcast.service.cargo.ExtCproductService;
import cn.itcast.service.cargo.FactoryService;
import cn.itcast.utils.FileUploadUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;

import java.util.Date;
import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping("/cargo/extCproduct")
public class ExtCproductController extends BaseController {

    @Reference
    private ContractService contractService;
    @Reference
    private ContractProductService contractProductService;
    @Reference
    private FactoryService factoryService;
    @Reference
    private ExtCproductService extCproductService;

    @RequestMapping(value = "/list",name ="查询某个货物下附件的数据")
    public String findAll(String contractId,String contractProductId,@RequestParam(defaultValue = "1",name = "page") int pageNum, @RequestParam(defaultValue = "10") int pageSize){

//        页面中需要用到的数据：
//        1、货物下附件列表数据 2、用来生产附件的工厂列表 3、隐藏域中需要的货物id和合同id （这两个id从参数中获取）

//        1、货物下附件列表数据PageInfo   select * from 附件 where 货物id=??
        ExtCproductExample example = new ExtCproductExample();
        example.createCriteria().andContractProductIdEqualTo(contractProductId);
        PageInfo<ExtCproduct> pageInfo = extCproductService.findAll(example,pageNum,pageSize);
        request.setAttribute("page",pageInfo);
//        2、用来生产附件的工厂数据 通过ctype查询  select * from co_factory where ctype='附件'
        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

//        3、货物id和合同id，为了保存附件时关联合同和货物
        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);

        return "cargo/extc/extc-list";
    }


//    toUpdate.do?id=646c181d-a7ea-4d7f-8bb5-87fc55eed452&contractId=0cbf2719-99fe-40f8-a417-acebe864bf40&contractProductId=32b1a17f-a4ca-4638-baf2-576863290611
    @RequestMapping(value = "/toUpdate",name ="进入编辑附件页面")
    public String toUpdate(String id,String contractId,String contractProductId){
//        查询当前附件
        ExtCproduct extCproduct = extCproductService.findById(id);
        request.setAttribute("extCproduct",extCproduct);


        FactoryExample factoryExample = new FactoryExample();
        factoryExample.createCriteria().andCtypeEqualTo("附件");
        List<Factory> factoryList = factoryService.findAll(factoryExample);
        request.setAttribute("factoryList",factoryList);

        request.setAttribute("contractId",contractId);
        request.setAttribute("contractProductId",contractProductId);



        return "cargo/extc/extc-update";
    }


    @Autowired
    private FileUploadUtil fileUploadUtil;

    @RequestMapping(value = "/edit",name ="保存附件方法")
    public String edit(ExtCproduct extCproduct, MultipartFile productPhoto){
//        处理图片
        String imagePath = null;
        try {
            imagePath = fileUploadUtil.upload(productPhoto);
        } catch (Exception e) {
            System.out.println(e.getMessage());

        }
        extCproduct.setProductImage(imagePath);

        if(StringUtils.isEmpty(extCproduct.getId())){
            extCproduct.setId(UUID.randomUUID().toString());
            extCproduct.setCompanyId(getCompanyId());
            extCproduct.setCompanyName(getCompanyName());
            extCproduct.setCreateTime(new Date());
            extCproductService.save(extCproduct);
        }else{
            extCproductService.update(extCproduct);
        }
        return "redirect:/cargo/extCproduct/list.do?contractId="+extCproduct.getContractId()+"&contractProductId="+extCproduct.getContractProductId();  // 重定向到货物的列表页面
    }

    //    /cargo/contractProduct/list.do?contractId=a2d5f5d4-59a9-4e70-bd76-a30f142afd9b
    @RequestMapping(value = "/delete",name ="删除附件方法")
    public String delete(String id,String contractId,String contractProductId){
        extCproductService.delete(id);
        return "redirect:/cargo/extCproduct/list.do?contractId="+contractId+"&contractProductId="+contractProductId;
    }

}
